package com.yang.service;

import com.yang.dao.BrandDao;
import com.yang.pojo.Brand;
import com.yang.pojo.good;

import java.sql.SQLException;
import java.util.ArrayList;

public class BrandService {
    BrandDao brandDao = new BrandDao();
    //查询品牌的所有信息
    public ArrayList<Brand> findBrand() throws SQLException {
        ArrayList<Brand> brands = brandDao.selectBrand();
        return brands;
    }

    //根据id删除品牌信息，需要保证这个品牌下没有商品信息方可删除
    public boolean deleteBrandById(int brandId) throws SQLException {

        //根据当前品牌id查询商品信息
        ArrayList<good> goods = brandDao.selectGoodByBrandId(brandId);
        if(goods.size() > 0){
            return false;
        }else {
            //先删除品牌与分类之间的关系，在删除品牌
            int j = brandDao.deleteBrandAndCategoryByBrandId(brandId);
            int i = brandDao.deleteBrandById(brandId);
            boolean b = i + j>1;
            return b;
        }


    }

    //状态启用
    public boolean enableCategoryStatus(int brandId) throws SQLException {
        return changeStatusById(1,brandId);
    }

    //状态停用
    public boolean disableCategoryStatus(int brandId) throws SQLException {
        return changeStatusById(0,brandId);
    }


    //根据id修改状态
    private boolean changeStatusById(int brandStatus,int brandId) throws SQLException {
        int i = brandDao.updateBrandStatus(brandStatus, brandId);
        boolean b = i > 0;
        return b;
    }

    //保存品牌信息保存品牌与分类的关系
    public boolean saveBrandAllAndCategory(String brandName,String brandLogo,String brandDesc,int categoryId) throws SQLException {
        int BrandId = brandDao.insertBrand(brandName, brandLogo, brandDesc);
        int i = brandDao.insertCategoryAndBrand(categoryId, BrandId);
        boolean b = i > 0;
        return b;

    }

    //根据id查询品牌信息
    public Brand findBrandById(int brandId) throws SQLException {
        Brand brand = brandDao.selectBrandById(brandId);
        return brand;
    }

    //修改品牌信息
    public boolean updateBrand(String brandName,String brandLogo,String brandDesc,int brandId) throws SQLException {
        int i = brandDao.updateBrandById(brandName, brandLogo, brandDesc, brandId);
        boolean b = i>0;
        return b;
    }

    //根据分类id查询品牌列表
    public ArrayList<Brand> findBrandByCategoryId(int categoryId) throws SQLException {
        ArrayList<Brand> brands = brandDao.selectBrandByCategoryId(categoryId);
        return brands;
    }

}
